package com.lyb.server.hb.demo;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.lyb.server.hb.utils.ConverterUtil;
import com.lyb.server.hb.utils.RSAUtil;

import java.util.HashMap;
import java.util.Map;


/**
 * 收银员签到签退Demo示例
 * @author Administrator
 *
 */
public class CashierDemo {

//	private static final String url = "http://127.0.0.1:9901/order/api";//请求地址
private static final String url = "https://mamipay.com/api-order-receipt/order/api";//请求地址--测试
//	private static final String url = "https://pay.erpyt.com/api-order-receipt/order/api";//请求地址--生产

	
	// 加密验签的公钥--测试
	private static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDWQWY3KvQ9iBfUHQTF2L5YQsaDR+pWJ/3dOoMuXr6rQ3qQSHxssYl6FLjmMBJ6LxVEDRnA+K81lm1QPHEs5cirF2MfJ0Een2OoNG3iDk3VIcfAVZyPwBKHct9RjM95t3mr6g0WsLxnTsWZ0mQuEdS20KYkk2uGFG4zrd/NFnB3HQIDAQAB";
	// 签名解密的私钥--测试
	private static final String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBANZBZjcq9D2IF9QdBMXYvlhCxoNH6lYn/d06gy5evqtDepBIfGyxiXoUuOYwEnovFUQNGcD4rzWWbVA8cSzlyKsXYx8nQR6fY6g0beIOTdUhx8BVnI/AEody31GMz3m3eavqDRawvGdOxZnSZC4R1LbQpiSTa4YUbjOt380WcHcdAgMBAAECgYAo0qcuJcIJo0+e+7R4GfcSjA6U3vP4XIBTUZ3DDQxZR28n5JfIgf9GwoIEYe5zV1s+7xwUF44SsMm9Ovb5bgLQtU9w28VPepEQ4p2dgx1dFU8lCnmewVCuGTaxAwvRL9DNfa++PxXiLZBrXcNykcW+0/iH gF7+dt1qgKGHnzbTgQJBAO2WvZU7x3/Cn5JEQJHDFJ+4RTfwB4ySl8GLTR37G3+E tAADA+W5Uz1l/C6CJKPMCp6iyyGgD+/twekRJFKKVeECQQDm28YvwSsW1+LQpjw3 Kwb6UQl24dSsJB92g6Mjl90mWLN4hMuyrJ5xTrQgWaz8H0C2dvzXktxYNWGsnXNtERC9AkASOz//BKBRcHjYO36M8OBTRUuieDvMEK+ufWcEhvFcPq2lVpXk+eymSH4qf3gaTZz/AVT/Fo5ChgccQGF+J/lhAkA49bzeh4DEMBwMcxekXP9pv5jgNjQtDPSeXzhfTdcI4RXXBJzlxuWI+2xCFx4xTty+qHkWD2xCb1lcJ7UVEgLZAkAYT3zyA5jc0/fdLnYCYF3457WPJr4hlbv5S2RGDrRi+v5dlbD1OJQbVmCEdetkaCBIiehDIkatqMITTctnJZQL";

	private static String idNo = "888888";//厂商识别号
	private static String merchantCode = "HB_M202303050001";//商户标识
	private static String appId = "wx0db32e374d18dc08";//微信公众号appId
	private static String appletsAppId = "wx0db32e374d18dc08";//微信小程序appId


	public static void main(String[] args) throws Exception {

		cashierSignInOut();//收银员签到签退

		System.out.println("main方法执行完成...");
	}


	/**
	 * 获取系统公钥(航邦公钥)
	 * 公钥是不变的,获取后请自行保存
	 * @throws Exception
	 */
	public static String getPublicKey() {
		Map<String, Object> param = new HashMap<>();
		String res = HttpUtil.post(url+"/get_public_key", param, 60000);
		JSONObject resJsonStr = ConverterUtil.toJSON(res);
		System.out.println("=============《获取系统公钥 响应数据》==============\n"+resJsonStr);
		/**
		 * 响应示例
		 * {"code":"0000","msg":"success","data":"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiSecPJOQnhaepE1v1lTfUdW5paAbO37Ff9G6GeN25REikHYqKKW1aRNYXfrhLpty8oRcOZWd98Im7o/kQDKpcemlsRkgqRvqOqaEyy8DFNRXR0kJ9wTPafgjho/GN71K3G9uHhWTX3ejbdW5s37a60t/7Cv0utatRs1TLpiFMBFULEFYcEDGuwuTUASSORRH6ymcxHDdUb+Tc0S0AZIg7zdS7cKqNsuZAPglBLgZZOAtU/xgERSrkInCvXh/V/1AYG96jTCQ0+Ky/kXFzd+p8r21E+HGm9rNP3kcMhOWaBYi39zE2Bw31DojE4PMShY08tYZqT0nOIiLoZypLnrQ5wIDAQAB","sign":""}
		 */
		return (String) resJsonStr.get("data");
	}

	/**
	 * TODO 商户没有开启签到模式,收银员无需进行签到与签退
	 * 收银员签到签退(商户开启签到模式后,收银员必须进行签到才能进行收款退款操作)
	 * 注意签到与签退都请求此接口,两个状态自动转换
	 */
	public static void cashierSignInOut() throws Exception {
		JSONObject jsonParams = new JSONObject();
		JSONObject data = new JSONObject();
		data.put("idNo", idNo);//厂商识别号
		data.put("merchantCode", merchantCode);//商户标识
		data.put("cashierId", "1635575067023831041");//收银员id(由我方提供或登录商户后台自行查找)

		System.out.println("data序列化后:"+data.toString());
		String sign = RSAUtil.sign(data.toString(), privateKey);//使用自己的私钥进行签名
		System.out.println("sign签名：\n" + sign);
		jsonParams.put("data", data.toString());
		jsonParams.put("sign", sign);
		//使用系统公钥(航邦公钥)加密
		String encryptBody = RSAUtil.encrypt(jsonParams.toString(), getPublicKey());
		Map<String, Object> param = new HashMap<>();
		param.put("param", encryptBody);
		String res = HttpUtil.post(url+"/cashier/signInOut", param, 60000);
		JSONObject resJson = ConverterUtil.toJSON(res);
		System.out.println("=============《响应数据》==============\n"+resJson);
		if("0000".equals(resJson.get("code")) && resJson.get("data")!=null){
			String encryptDataStr = resJson.get("data").toString();//加密的data数据
			String sign2 = resJson.get("sign").toString();//签名串
			//使用自己的私钥进行解密数据   注: 数据传输时 “+” 号会替换为空格， 解密时 需把空格替换回“+”号
			String paramJsonStr = RSAUtil.decrypt(encryptDataStr.replaceAll(" ", "+"), privateKey);
			System.out.println("解密后的data\n"+paramJsonStr);
			/**
			 * 解密后的data示例
			 * {"redirectUrl":"http://127.0.0.1:9901/web/pay/blank.html?tk=aad02bfcb9964c4c9a18e8fa83fbc5a1"}
			 **/
			//将解密出来的数据使用系统公钥(航邦公钥)进行验签
			boolean verify = RSAUtil.verifySign(paramJsonStr, sign2, getPublicKey());
			System.out.println("验签结果："+verify);
		}
	}

}
